<template>
  <div class="search-history">
    <van-cell title="搜索历史">
      <template v-if="isDeleteShow">
        <span @click="clearAll">全部删除</span>
        &nbsp;&nbsp;
        <span @click="isDeleteShow = false">完成</span>
      </template>
      <van-icon v-else name="delete" @click="isDeleteShow = true"/>
    </van-cell>
    <van-cell @click="onHistoryClick(item, index)"
     :title="item" v-for="(item, index) in searchHistories" :key="index">
      <van-icon v-show="isDeleteShow" name="close" />
    </van-cell>
  </div>
</template>

<script>
export default {
  name: 'SearchHistory',
  props: {
    searchHistories: {
      type: Array,
      default: () => []
    }
  },
  data () {
    return {
      // 在 data 中添加一个数据用来控制删除相关元素的显示状态
      isDeleteShow: false,
      searchHistoriesClone: this.searchHistories
    }
  },
  methods: {
    onHistoryClick (item, index) {
      if (this.isDeleteShow) {
        this.searchHistoriesClone.splice(index, 1)
      } else {
        // 非删除状态，直接进入搜索
        this.$emit('search', item)
      }
    },
    clearAll () {
      this.$emit('clearAll')
    }
  }
}
</script>

<style scoped lang="less"></style>
